<?php
session_start();
require_once( 'vehicle_db.php' );
 
$m_db = new M_Car();
$user_id = $_SESSION['userid'];
function MonthThai($m){
 $thaimonth=array("ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค."," มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
  
 return $thaimonth[$m];

}
function type($i){
	$type = array(  "เดือน","วัน" ,"ชม" , "เทอม");
	
	return $type[$i];
}
if (isset($_POST['m'])  &&  $_POST['m']!=""){
    if ($_POST['m']== 'load'){
      $result=$m_db->loadData();
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['id'],$row['license'],$row['detail'], $row['amount'],$row['status']) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
	}else if ($_POST['m']=='load_manage'){
		$year= $_POST['y'];
		$term=$_POST['t'];
		$status = $_POST['s'];
		
		$result=$m_db->loadCarMange($year,$term,$status);
		  $rows=$m_db->getRowCount();
		  $i=1;
		  $html="";
		  if ($rows>0){
			   $data = array();
				while($row = $result->FetchRow()){
				   $val = $m_db->loadRoute($row['id']);
					$col1="";
					$col2="";
					$col3="";
				   while($detail = $val->FetchRow()){
					$col1 .="<span style='display:block'>".$detail['route']."</span>";
					$col2 .="<span style='display:block'>".$detail['route_time']."</span>";
					$col3 .="<span style='display:block;'>".$detail['start_day']. " ". MonthThai($detail['start_month']-1)." ".$detail['start_year']."</span>";
				   }
				   $price = number_format(  $row['cost'],0,'.',','). " บ. / ". $row['type_name']." <span style='display:block;'>".$row['remark']."</span>";
				   $data[] = array(0,$row['id'],$row['detail'].'<br/>'.$row['cost_detail'],$col1,$col2,$col3, $price ,$row['status']) ;
				}
			}else{
				$data = array();
			}
		
			$arr = array('aaData'=>$data);
			
			echo json_encode($arr);
	
    }else if ($_POST['m']=='load_car'){
		$id=$_POST['id'];
		$result=$m_db->loadRoute($id);
		$rows=$m_db->getRowCount();
		$i=1;
		
        $data = array();
		if ($rows>0){
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['route'],$row['route_time'],$row['startdate']) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
	
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['license']=  $_POST['license'];
		$ar['detail'] = $_POST['detail'];
		$ar['status'] = $_POST['s'];
		$ar['amount'] = $_POST['amt'];
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
	 
		$id=$m_db->InsertData($ar);
		
	}else if ($_POST['m']=='add_detail'){
        $ar = array();
		$val = $_POST['list'];
		$ar = $_POST;
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		
		$id=$m_db->InsertDataDetail($ar);

		if (count($val)>0){
			foreach($val as $row){
				list($d,$m,$y) = explode('/', $row['start_date1']);
				$row['start_date'] = $y.'/'.$m.'/'.$d;
				$row['car_manage_id'] = $id;
				$m_db->insertRoute($row);
			}
		}
	}else if ($_POST['m']=='edit_detail'){
        $ar = array();
		$val = $_POST['list'];
		$ar = $_POST;
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		$id=$_POST['id'];
		$m_db->updateDataDetail($id,$ar);
		
		$m_db->deleDetail($id);
		
		if (count($val)>0){
			foreach($val as $row){
				list($d,$m,$y) = explode('/', $row['start_date1']);
				$row['start_date'] = $y.'/'.$m.'/'.$d;
				$row['car_manage_id'] = $id;
				$m_db->insertRoute($row);
			}
		}
    	
    }else if ($_POST['m']=='edit'){
		$ar = array();
		$ar['license']=  $_POST['license'];
		$ar['detail'] = $_POST['detail'];
		$ar['status'] = $_POST['s'];
		$ar['amount'] = $_POST['amt'];
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] = $user_id;
	 
		$strcipt = $m_db->updateData($_POST['id'],$ar);
		// echo json_encode($strcipt);
	}
	else if ($_POST['m']=='del'){
         $id=$_POST['id'];
		// $type->setTable($_POST['tb']);  
		 $m_db->deleData($id);
	}else if ($_POST['m']=='delCar'){
		$id=$_POST['id'];
		// $type->setTable($_POST['tb']);  
		 $m_db->deleManage($id);
    }else if ($_POST['m']=='detail'){
		$id=$_POST['id'];
		$result=$m_db->loadCarMange('','','0',$id);
		$row=$result->FetchRow();
		echo json_encode($row);
	}
}else{

	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if ($_GET['m'] == 'cbo_car'){
			echo json_encode( $m_db->getDropdownCar());
		}else if ($_GET['m']=='year'){
		  echo json_encode($m_db->getYear_Term());
		}else if ($_GET['m'] =='curryear'){
			echo $m_db->getCurrentYear();	
		}else if ($_GET['m']=='c'){
			echo  json_encode( $m_db->getDropdownCarDetail($_GET['i']));
		}else if ($_GET['m']=='assets'){
			echo  json_encode($m_db->getAssetsPayment());
		}else if ($_GET['m'] =='term'){
		  echo json_encode($m_db->getTerm());
		}else if ($_GET['m'] =='currterm'){
			echo $m_db->getCurrentTerm();
		}else if ($_GET['m']=='feeType'){
			echo  json_encode($m_db->getPaymentTypeService());
		}
	} 
   
}
 
?>